//
//  EMProgressGradientView.m
//  EasyMoney
//
//  Created by zh on 2020/1/12.
//  Copyright © 2020 zh. All rights reserved.
//


#import "EMProgressGradientView.h"

@implementation EMProgressGradientStyleModel

@end

@interface EMProgressGradientView ()

@property (nonatomic, strong) EMProgressGradientStyleModel *style;

//背景layer
@property (nonatomic, strong) CAShapeLayer *backgroundLayer;

//进度条layer
@property (nonatomic, strong) CAShapeLayer *progressLayer;

@end

@implementation EMProgressGradientView

- (void)layoutSubviews{
    [super layoutSubviews];

}


- (instancetype)initWithFrame:(CGRect)frame ProgressGradientModel:(EMProgressGradientStyleModel *)style{
    if (self = [super initWithFrame:frame]) {
        self.style = style;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{

    self.backgroundLayer = [CAShapeLayer layer];
    self.backgroundLayer.lineCap = @"round";
    self.backgroundLayer.lineWidth = self.style.lineWidth;
    self.backgroundLayer.fillColor = [UIColor clearColor].CGColor;
    self.backgroundLayer.strokeColor = self.style.backgroundColor.CGColor;

    self.progressLayer = [CAShapeLayer layer];
    self.progressLayer.lineWidth = self.style.lineWidth;
    self.progressLayer.lineCap = @"round";
    self.progressLayer.fillColor = [UIColor clearColor].CGColor;
    self.progressLayer.strokeColor = [UIColor whiteColor].CGColor;
    
    [self.layer addSublayer:self.backgroundLayer];
    [self.layer addSublayer:self.progressLayer];
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:self.style.origin radius:self.style.radius startAngle:self.style.startAngle endAngle:self.style.endAngle clockwise:YES];

    self.backgroundLayer.path = progressPath.CGPath;
    self.progressLayer.path = progressPath.CGPath;

      [self gradient];
      self.progressLayer.strokeEnd = 0;
}

- (void)gradient {

    CALayer *gradientLayer = [CALayer layer];

    CAGradientLayer *rightGradient = [CAGradientLayer layer];
    rightGradient.frame = CGRectMake(-self.style.lineWidth/2, -self.style.lineWidth/2, self.bounds.size.width + self.style.lineWidth, self.bounds.size.height + self.style.lineWidth);
    rightGradient.colors = @[(id)self.style.startColor.CGColor,(id)self.style.endColor.CGColor];
    [gradientLayer addSublayer:rightGradient];

    [gradientLayer setMask:self.progressLayer];
    [self.layer addSublayer:gradientLayer];

}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;

    [self.progressLayer removeAllAnimations];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = @0;
    animation.toValue = @(progress);
    animation.duration = 1.5f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.removedOnCompletion = NO;
    animation.repeatCount = 1;
    animation.fillMode = kCAFillModeForwards;
    [self.progressLayer addAnimation:animation forKey:@"strokeEnd"];
}
@end
